// 删除每日记录云函数
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const { date_str } = event

  try {
    // 查找并删除指定日期的记录
    const result = await db.collection('daily_records')
      .where({
        openid: wxContext.OPENID,
        date_str: date_str
      })
      .remove()

    console.log('删除记录结果:', result)

    return {
      status: 'success',
      message: '记录删除成功',
      data: {
        deleted: result.stats.removed
      }
    }

  } catch (error) {
    console.error('删除记录失败:', error)
    return {
      status: 'error',
      message: '删除记录失败',
      error: error.message
    }
  }
}
